'use strict';

/* Controllers */



function oneToManyCtrl($scope,$stateParams,$http, $location) {
	/*
  $http.get('user/' + $stateParams.id).success(function(data) {
    $scope.user = data;
 
    $('#datetimepicker').datetimepicker({
        format: 'dd/MM/yyyy hh:mm:ss'
      });
  });
  */

    $('#datetimepicker').datetimepicker({
        format: 'dd/MM/yyyy hh:mm:ss'
      });
    
  $scope.over = function(user){
	  alert(user.name);
  };
  $scope.leave = function(user){
	  alert(user.name);
  };
  
  $scope.isCollapsed = false;
  nexus.scheduleForm=$scope;
  
  $scope.schedule = function (){
		
		var ploc=$location.url();
	
var startDate = $('#datetimepicker').data('datetimepicker').getLocalDate();
var endDate = $('#datetimepicker').data('datetimepicker').getLocalDate();
if($scope.selectedPeriod.id==undefined) {
	endDate.setMinutes(endDate.getMinutes()+$scope.selectedPeriod[0]);
} else {
	endDate.setMinutes(endDate.getMinutes()+$scope.selectedPeriod.id);
}
var reqData = {"description" : $('#description').val(), "startTime": startDate, "endTime": endDate, 
		"categoryName": $('#selectedCategory option:selected').text(), "numberOfSeats": $('#seats option:selected').text(), "price":$('#price').val()};



$http.post('counsel/'+ $scope.user.id+'/oneToMany', reqData).success(function(data) {
	//myUserRef.set({ message: $('#scheduleMessage').val()});
	$location.path('/dashboard/sessions');
});
  };
	
  $scope.sendMessage = function (){
	
			var ploc=$location.url();
			if(nexus.headerScope.notLogin){
				nexus.headerScope.turl=function(){
					//$location.path();
					$location.url(ploc);
				};
				nexus.headerScope.showLogin();
				return;
			}

	 var reqData = {"message" : $('#textmessage').val(), "toUser":{"id":$stateParams.id}};
	 var userListRef = new Firebase("https://adviseme.firebaseio.com/");
	 var myUserRef = userListRef.child($stateParams.id);
	 
	 $http.post('message', reqData).success(function(data) {
		 
		 myUserRef.set({ message: $('#textmessage').val()});
		 $('#textmessage').val('');
		 alert("Message Posting "+data);
		//$location.path('/dashboard');
	});
	
		//}
	};
	
	$scope.periods = [{id:15, name:"15 min"}, {id:30,name:"30 min"},{id:60 ,name:"1 hr"}];
	$scope.seats = [{id:10, name:"10"}, {id:50,name:"50"},{id:100 ,name:"100"},{id:500 ,name:"500"},{id:1000 ,name:"1000"},{id:999999 ,name:"unlimited"}];
	$scope.selectedPeriod = $scope.periods[0];
}
oneToManyCtrl.$inject = ['$scope','$stateParams','$http', '$location'];//inject required



